package org.wattdepot.cli.command;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.CommandLineInterface;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Writes out an html file that will display a chart illustrating the power for the source over 
 * the interval.
 * 
 * @author Anthony Xu & John Mack
 *
 */
public class ChartPowerCommand implements WattDepotCliCommand {

  /**
   * Writes out an html file that will display a chart illustrating the power for the source over 
   * the interval.
   * 
   * @param cli WattDepotClient with hostUri = http://server.wattdepot.org:8182/wattdepot/
   * @param commands user's command line input
   * @return string to output
   */
  public String doCommand(CommandLineInterface cli, List<String> commands) {
    String generatedOrConsumed = commands.get(2);
    String source = commands.get(3);
    String startDay = commands.get(4);
    String endDay = commands.get(5);
    int minutes = Integer.parseInt(commands.get(7));
    String file = commands.get(9);
    WattDepotClient client = cli.getClient();   
    double power = 0, minPower = Double.MAX_VALUE, maxPower = Double.MIN_VALUE;    
    XMLGregorianCalendar startTime = null, endTime = null, timestamp;      
    int maxUriLength = 2048;    
    StringBuffer chartUri = new StringBuffer(maxUriLength);    
    try {
      startTime = Tstamp.makeTimestamp(startDay);
      endTime = Tstamp.makeTimestamp(endDay); 
    }
    catch (Exception e) {
      e.printStackTrace();
    }          
    chartUri.append("<img src=\"http://chart.apis.google.com/chart?chs=250x100&cht=lc&chd=t:");    
    timestamp = startTime;    
    while (Tstamp.lessThan(timestamp, endTime)) {
      try {
        // Convert to megawatts
        if ("generated".equals(generatedOrConsumed)) {
          power = client.getPowerGenerated(source, timestamp) / 1000000;
        }
        else if ("consumed".equals(generatedOrConsumed)) {
          power = client.getPowerConsumed(source, timestamp) / 1000000; 
        } 
      }
      catch (Exception e) {
        e.printStackTrace();
      }     
      chartUri.append(String.format("%.1f,", power));      
      if (power < minPower) {        
        minPower = power;      
      }      
      if (power > maxPower) {        
        maxPower = power;      
      }      
      timestamp = Tstamp.incrementMinutes(timestamp, minutes);    
    }    
    // Delete trailing ','    
    chartUri.deleteCharAt(chartUri.length() - 1);    
    chartUri.append(String.format("&chds=%.1f,%.1f&chxt=y&chxr=0,%.1f,%.1f>", minPower, maxPower,  
      minPower, maxPower)); 
    try {
      BufferedWriter outputFile = new BufferedWriter(new FileWriter(file));
      outputFile.write(chartUri.toString()); 
      outputFile.close();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    return "Wrote: \\build\\jar\\" + file;
  }
  
}